﻿using Seggu.Daos.Interfaces;
using Seggu.Data;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Transactions;
using System.Data.Entity;

namespace Seggu.Daos
{
    public class ProducerCodeDao: GenericDao<ProducerCode>, IProducerCodeDao
    {
        public override void Save(ProducerCode obj)
        {
            var pc = container.ProducerCodes.SingleOrDefault(x => x.ProducerId == obj.ProducerId &&
                x.CompanyId == obj.CompanyId);
            if (pc != null) return;
            using (var scope = new TransactionScope())
            {
                Set.Add(obj);
                container.SaveChanges();
                scope.Complete();
            }
        }
        public override void Delete(ProducerCode obj)
        {
            using (var scope = new TransactionScope())
            {
                var pc = container.ProducerCodes.Single(x => x.ProducerId == obj.ProducerId &&
                    x.CompanyId == obj.CompanyId && x.Code == obj.Code);
                var entry = this.container.Entry(pc);
                entry.State = EntityState.Deleted;
                this.container.SaveChanges();
                scope.Complete();
            }
        }
    }
}
